//
//  YANewFeatureController.swift
//  KeepCliming
//
//  Created by 鱼龙之梦 on 15/9/5.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit

private let YANewFeatureCellID = "YANewFeatureCellID"
private let YANewFeatureCount = 4
class YANewFeatureController: UICollectionViewController {

    init(){
        super.init(collectionViewLayout:UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: YANewFeatureCellID)
        //准备布局数据
        prepareLayout()
    }
    private func prepareLayout(){
        //准备当前的流布局属性
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return YANewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YANewFeatureCellID, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
    
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == YANewFeatureCount - 1 {
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            cell.showStartButton()
        }
    }
}
//在这里自定义只有本控制器的Cell :一张主题图片 一个进入按钮
private class NewFeatureCell:UICollectionViewCell{
   private lazy var iconView = UIImageView()
   private lazy var startButton:UIButton = {
    let button = UIButton();
    button.setTitle("开始体验", forState:UIControlState.Normal)
    button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
    button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
    button.sizeToFit()
    button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
    return button
   }()
    //图像索引属性
    private var imageIndex:Int = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    @objc func clickStartButton(){
        //点击后发送切换根控制器的的通知
        NSNotificationCenter.defaultCenter().postNotificationName(YARootViewControllerSwitchNotification, object: nil)
    }
    //动画显示启动按钮
    private func showStartButton(){
      startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                //
        }
    }
    //设置界面元素
    private func setupUI(){
        addSubview(iconView)
        addSubview(startButton)
       // iconView.frame = bounds
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(-16)-[v]-(-16)-|", options: [], metrics: nil, views: ["v" : iconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(-8)-[v]-(-8)-|", options: [], metrics: nil, views: ["v" : iconView]))
        startButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
    }
    //重写构造函数
    override init(frame:CGRect)
    {
        super.init(frame:frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
}
